 package com.chenxiang.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chenxiang.dto.User;
import com.chenxiang.dto.UserQueryCondition;
import com.fasterxml.jackson.annotation.JsonView;

/**
 * @author chenxiang
 * @date 2018/08/28
 */
 @RestController
 @RequestMapping("/user")
public class UserController {
     
     @PostMapping
     public User create(@Valid @RequestBody User user) {

         System.out.println(user.getId());
         System.out.println(user.getUsername());
         System.out.println(user.getPassword());
         System.out.println(user.getBirthday());

         user.setId("1");
         return user;
     }

     @PutMapping("/{id:\\d+}")
     public User update(@Valid @RequestBody User user, BindingResult errors) {

         System.out.println(user.getId());
         System.out.println(user.getUsername());
         System.out.println(user.getPassword());
         System.out.println(user.getBirthday());

         user.setId("1");
         return user;
     }

     @DeleteMapping("/{id:\\d+}")
     public void delete(@PathVariable String id) {
         System.out.println(id);
     }

     @GetMapping
     @JsonView(User.UserSimpleView.class)
     public List<User> query(UserQueryCondition condition,
             @PageableDefault(page = 2, size = 17, sort = "username,asc") Pageable pageable) {

         System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE));

         System.out.println(pageable.getPageSize());
         System.out.println(pageable.getPageNumber());
         System.out.println(pageable.getSort());

         List<User> users = new ArrayList<>();
         users.add(new User());
         users.add(new User());
         users.add(new User());
         return users;
     }

     @GetMapping("/{id:\\d+}")
     @JsonView(User.UserDetailView.class)
     public User getInfo(@PathVariable String id) {
//       throw new RuntimeException("user not exist");
         System.out.println("进入getInfo服务");
         User user = new User();
         user.setUsername("tom");
         return user;
     }
}
